jQuery.noConflict();
(function ($) {
    "use strict";
    var HEADERNUM = 0;
    kintone.events.on("app.record.index.show", function (event) {
        $("#submit").click(function () {
            var headers = {};
            var $reqURL = $("#req-url").val();
            var $reqBody = ($("#req-body-txt").val()) ? $("#req-body-txt").val() : {};
            $('.header-box').each(function () {
                var key = $(this)[0].children[0].value;
                var val = $(this)[0].children[2].value;
                headers[key] = val;
            });
            if (!$("#sub-domain").val()) {
                swal("Error", "请输入子域名", "error");
            } else if (!checkURL($reqURL)) {
                swal("Error", "URL输入不正确", "error");
            } else {
                var tempJson = '';
                kintone.proxy($reqURL, $("#req-method").val(), headers, $reqBody, function (resp) {
                    try {
                        tempJson = JSON.parse(resp);
                        tempJson = JSON.stringify(tempJson, null, "    ");
                    } catch (e) {
                        tempJson = "发送的数据有错";
                    }
                    $("#resp-body").val(tempJson);
                }, function (err) {
                    try {
                        tempJson = JSON.parse(err);
                        tempJson = JSON.stringify(tempJson, null, "    ");
                    } catch (e) {
                        tempJson = "发送的数据有错";
                    }
                    $("#resp-body").val(tempJson);
                });
            }
        });
        $("#sub-domain").keyup(function () {
            if ($(this).val()) {
                $("#req-url").prop("disabled", false);
                $("#req-url").removeClass("disable-input");
            } else {
                $("#req-url").prop("disabled", true);
                $("#req-url").addClass("disable-input");
            }
            createURL($("#sub-domain").val(), $("#req-domain").val(), $("#kintone-api").val(), checkGuest());
        });
        $("#kintone-api, #req-domain, #guest-check").change(function () {
            createURL($("#sub-domain").val(), $("#req-domain").val(), $("#kintone-api").val(), checkGuest());
        });
        createHeaderLine();
    });
    //Check the checkbox for guest space.
    function checkGuest() {
        if ($("#guest-check:checked").val()) {
            return true;
        } else {
            return false;
        }
    }
    //Check the URL user input.
    function checkURL(url) {
        if (url.match(/cybozu.com|kintone.com|cybozu.cn/)) {
            return true;
        } else {
            return false;
        }
    }
    //Create Line for headers.
    function createHeaderLine() {
        var id = 'heder-box' + HEADERNUM;
        HEADERNUM++;
        var $keyValueSet = $('<p />')
            .attr('id', id)
            .addClass('header-box');
        var $keyBox = $('<input />')
            .attr('type', 'text')
            .attr('size', '15%')
            .attr('placeholder', 'key')
            .attr('autocomplete', 'on')
            .attr('list', 'head-key');
        var $splitEl = $('<span />')
            .text(':')
            .addClass('split-el');
        var $valueBox = $('<input />')
            .attr('type', 'text')
            .attr('size', '20%')
            .attr('placeholder', 'value')
            .attr('autocomplete', 'on')
            .attr('list', 'head-value');
        var $addPlusSp = $('<span />')
            .attr('clss', 'fa fa-plus')
            .addClass('plus-icon')
            .click(createHeaderLine);
        var $addPlusEle = $('<i />')
            .addClass('fa fa-plus');
        var $addMinusSp = $('<span />')
            .attr('clss', 'fa fa-plus')
            .addClass('minus-icon')
            .click(deleteHeaderLine);
        var $addMinusEle = $('<i />')
            .addClass('fa fa-minus');
        $addPlusSp.append($addPlusEle);
        $addMinusSp.append($addMinusEle);
        $keyValueSet.append($keyBox);
        $keyValueSet.append($splitEl);
        $keyValueSet.append($valueBox);
        $keyValueSet.append($addPlusSp);
        $keyValueSet.append($addMinusSp);
        if ($('.header-box').length === 0) {
            $addMinusEle.css('display', 'none');
        }
        $('#req-headers').append($keyValueSet);
    }
    //Delete lines function.
    function deleteHeaderLine() {
        HEADERNUM--;
        $("#heder-box" + HEADERNUM).remove();
    }
    //Create URL for your request.
    function createURL(subdomain, domain, api, guest) {
        var reqURL = "";
        if (api === "other APIs" && !guest) {
            reqURL = "https://" + subdomain + "." + domain + "/k/v1/";
        } else if (api === "other APIs" && guest) {
            reqURL = "https://" + subdomain + "." + domain + "/k/guest/'SPACEID'";
        } else if (!guest) {
            reqURL = "https://" + subdomain + "." + domain + api;
        } else if (guest) {
            reqURL = "https://" + subdomain + "." + domain + "/k/guest/'SPACEID'" + api.replace(/\/k/g, "");
        }
        $("#req-url").val(reqURL);
        return reqURL;
    }
})(jQuery);
